<template>
	<w-collapse v-model="procedureInfocollapseVisible" disabled :showArrow="false" title="工序信息">
		<procedureBasicInfo
			ref="procedureBasicInfo"
			:visibleFlag="visibleFlag"
			:procedureNameList="procedureNameList"
			:procedureDetails="procedureDetails"
			:productionLineId="productionLineId"
			:routeDetails="routeDetails"
			:disabled="disabled"
			@on-change-procedure="procedureChange"
			@saveProcedure="saveProcedure"
		></procedureBasicInfo>
		<procedureUseInfo
			ref="procedureUseInfo"
			:disabled="disabled"
			:itemList="itemList"
			:useDetails="useDetails"
			:fSaveMain="fSaveProcedureUse"
			:procedureDetails="procedureDetails"
			@on-details-refresh="refreshProUse"
		></procedureUseInfo>
		<procedureProInfo
			ref="procedureProInfo"
			:disabled="disabled"
			:proDetails="proDetails"
			:fSaveMain="fSaveProcedurePro"
			:proItemList="proItemList"
			:procedureDetails="procedureDetails"
			@on-details-refresh="refreshPro"
		></procedureProInfo>
		<sop
			ref="sop"
			:procedureDetails="procedureDetails.routingSopDTO"
		></sop>
	</w-collapse>
</template>
<script>
import procedureBasicInfo from './ProcedureBasicInfo.vue'
import procedureUseInfo from './ProcedureUseInfo.vue'
import procedureProInfo from './ProcedureProInfo.vue'
import sop from './SOP.vue'
export default {
	name: 'procedureInfo',
	components: {
		procedureBasicInfo,
		procedureUseInfo,
		procedureProInfo,
		sop
	},
	props: {
		visibleFlag: Boolean,
		procedureNameList: {
			type: Array,
			default: () => {
				return []
			}
		},
		procedureDetails: {
			type: Object,
			default: () => {
				return {}
			}
		},
		itemList: {
			type: Array,
			default: () => {
				return []
			}
		},
		proItemList: {
			type: Array,
			default: () => {
				return []
			}
		},
		routeDetails: {
			type: Object,
			default: () => {
				return {}
			}
		},
		fSaveProcedureUse: Function,
		fSaveProcedurePro: Function,
		productionLineId: String,
		disabled: Boolean
	},
	data () {
		return {
			procedureInfocollapseVisible: true,
			useDetails: [],
			proDetails: []
		}
	},
	methods: {
		procedureChange (val, procedureId) {
			this.$emit('procedureChange', val, procedureId)
		},
		saveProcedure () {
			this.$emit('saveProcedure')
		},
		checkProcedureDetail () {
			return this.onValidateFormData()
		},
		onValidateFormData () {
			return new Promise(resolve => {
				this.$refs.procedureBasicInfo.validate().then(() => {
					resolve()
				}).catch(() => {
					this.$fxMessage.warning('请填写必填信息')
				})
			})
		},
		// 验证路由中 工序产出品 是不是产品
		checkProcedureHasProduct (currentRoute, materialId) {
			return new Promise((resolve, reject) => {
				const filterPro = this.proDetails.find(item => {
					if (item.processingFlag === 1 && item.materialId === materialId) {
						return item
					}
				})
				// 如果存在就提示不能再新建路由
				if (!(filterPro && currentRoute.subId === '')) {
					resolve()
				} else {
					reject(new Error(0))
				}
			})
		},
		checkSameProduct (currentRoute, materialId) {
			return new Promise((resolve, reject) => {
				const filterPro = this.proDetails.find(item => {
					if (item.processingFlag === 1 && item.materialId === materialId) {
						return item
					}
				})
				if ((filterPro || currentRoute.subId !== '')) {
					resolve()
				} else {
					reject(new Error(0))
				}
			})
		},
		// 刷新消耗品
		refreshProUse () {
			return this.$fxApi('routing.findListByParam')({
				query: {
					routingId: this.procedureDetails.id === 'temp' ? '' : this.procedureDetails.id,
					dataType: 1
				}
			}).then(res => {
				this.useDetails = res.map(d => {
					const filterItem = this.itemList.filter(item => {
						return item.id === d.materialId
					})
					return {
						code: d.materialCode,
						item: {
							code: d.materialCode,
							name: d.materialName
						},
						itemUnit: {
							id: d.materialUnitId,
							busUnit: d.unit
						},
						itemUnitList: filterItem[0].technologyMaterialUnitEntirys,
						...d
					}
				})
				return Promise.resolve(res)
			})
		},
		// 刷新产出品
		refreshPro () {
			return this.$fxApi('routing.findListByParam')({
				query: {
					routingId: this.procedureDetails.id === 'temp' ? '' : this.procedureDetails.id,
					dataType: 2
				}
			}).then(res => {
				this.proDetails = res.map(d => {
					const filterItem = this.proItemList.filter(item => {
						return item.id === d.materialId
					})
					return {
						code: d.materialCode,
						item: {
							code: d.materialCode,
							name: d.materialName
						},
						itemUnit: {
							id: d.materialUnitId,
							busUnit: d.unit
						},
						itemUnitList: filterItem[0].technologyMaterialUnitEntirys,
						...d
					}
				})
				return Promise.resolve(res)
			})
		},
		// 获取路由工序 SOP图片信息
		getProcedureSOPImg (routingId) {
			return this.$fxApi('routing.sopImgFindListByParam')({
				query: {
					routingId: routingId
				}
			}).then(res => {
				const productionUrls = res.filter(item => {
					return item.dataType === 2
				}) || []
				const expendableUrls = res.filter(item => {
					return item.dataType === 1
				}) || []
				this.$set(this.procedureDetails.routingSopDTO, 'productionUrls', productionUrls.map(item => {
					return item.url
				}) || [])
				this.$set(this.procedureDetails.routingSopDTO, 'expendableUrls', expendableUrls.map(item => {
					return item.url
				}) || [])
				return Promise.resolve(res)
			})
		},
		// 获取路由工序 附件列表
		getProcedureSOPFile (routingId) {
			return this.$fxApi('routing.findProcedureFile')({
				query: {
					routingId: routingId
				}
			}).then(res => {
				const fileUrls = res || []
				this.$set(this.procedureDetails.routingSopDTO, 'fileUrls', fileUrls.map(item => {
					return item.url
				}) || [])
				return Promise.resolve(res)
			})
		},
		pCheckDetailsUnSaving () {
			return Promise.all([this.$refs.procedureUseInfo.checkDetailsUnSaving(), this.$refs.procedureProInfo.checkDetailsUnSaving()])
		}
	}
}
</script>
<style lang="stylus" scoped>
@import '~$assets/stylus/varsty.styl'
.create-organ {
	flex: 1
	display: flex;
	flex-direction: column;
	overflow: auto;
}
</style>
